<?
/*
 *  GGB - Gelin's Guest Book.
 *  Copyright (C) 2006  Denis Nelubin aka Gelin
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  http://gelin.ru
 *  mailto:den@gelin.ru
 *
 *  Description:    Admin main page, dispatcher.
 *
 *  History:
 *  2006-08-05  Gelin   Initially created.
 *  2006-12-20  Gelin   added add message actions
 *  2006-12-31  Gelin   added reply/edit message actions
 *  2007-01-07  Gelin   added delete action
 *  2007-07-04  Gelin   Task #6  Add a posibility to view deleted messages in admin interface.
 *                      - added admin-view-deleted action
 *  2009-10-14  Gelin   added admin-view-new, admin-approve-submit actions
 */

header('Content-type: text/html; charset=UTF-8');

session_start();
require_once('lib/functions.php');

//define profile
$profile = getParameter('profile');
if (!isset($profile) || !is_readable("profile/$profile.php")) {
    $profile = 'default';
}
$_SESSION['profile'] = $profile;
require_once("profile/$profile.php");

require_once($config['require']['storage']);
require_once($config['require']['admin_formatter']);
require_once($config['require']['ban_checker']);
require_once($config['require']['logger']);

//create logger
$log = new Logger();
$log->restore();

//create storage
$storage = new Storage();

//create formatter
$format = new AdminFormatter();

//create ban checker
$banChecker = new BanChecker();

//dispatch by action
$action = getParameter('action');

//check login status
if (!($action == '' || $action == 'admin-login-submit')) {
    $admin = $_SESSION['admin'];
    //$error = '';
    if (!isset($admin['logged'])) {
        $log->error($lang['error.login.not_logged']);
        $action = NULL; //default action (enter login)
    } else if ((time() - $admin['logged']) > $config['admin']['timeout']) {
        $log->error($lang['error.login.timeout']);
        $action = NULL; //default action (enter login)
    } else {
        $admin['logged'] = time();  //updated logged time
    }
    $_SESSION['admin'] = $admin;
    //$log->save();
}

switch ($action) {
case 'admin-login-submit':
    require_once('action/admin-login-submit.php'); //process admin login form
    break;
case 'admin-view':
    require_once('action/admin-view.php');  //admin view of approved messages
    break;
case 'admin-view-new':
    require_once('action/admin-view-new.php');  //admin view of new messages
    break;
case 'admin-view-deleted':
    require_once('action/admin-view-deleted.php');  //admin view of deleted messages
    break;
case 'admin-add':
    require_once('action/admin-add.php');  //admin view of add message form
    break;
case 'admin-add-submit':
    require_once('action/admin-add-submit.php');  //submits new message added by admin
    break;
case 'admin-reply':
    require_once('action/admin-reply.php');     //view of reply form
    break;
case 'admin-edit':
    require_once('action/admin-edit.php');      //view of edit form
    break;
case 'admin-edit-submit':
    require_once('action/admin-edit-submit.php');   //submits modified message from reply or edit form
    break;
case 'admin-delete':
    require_once('action/admin-delete.php');      //view of delete form
    break;
case 'admin-delete-submit':
    require_once('action/admin-delete-submit.php');   //submits message ID to be deleted
    break;
case 'admin-approve-submit':
    require_once('action/admin-approve-submit.php');   //submits message ID to be approved
    break;
case 'admin-ban-view':
    require_once('action/admin-ban-view.php');   //dislays list of banned IPs
    break;
case 'admin-ban-add':
    require_once('action/admin-ban-add.php');   //view of ban form
    break;
case 'admin-ban-add-submit':
    require_once('action/admin-ban-add-submit.php');   //submits ban form
    break;
case 'admin-ban-delete-submit':
    require_once('action/admin-ban-delete-submit.php');   //deletes ban
    break;
default:
    require_once('action/admin-login.php'); //ask login/password
}

?>